import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_chip/config/Comm.dart';
import 'package:flutter_chip/utils/comm_utils.dart';
import 'package:flutter_chip/utils/toast_utils.dart';
import 'package:fradio/fradio.dart';
import 'package:fsuper/fsuper.dart';

class LedPage extends StatefulWidget {
  @override
  _LedPageState createState() => _LedPageState();
}

class _LedPageState extends State<LedPage> {
  var _currController = TextEditingController();
  var _codeController = ScrollController();

  Map<String, int> mapLeds = {
    'A': 0,
    'B': 0,
    'C': 0,
    'D': 0,
    'E': 0,
    'F': 0,
    'G': 0,
    'dp': 0
  };

  int jixing = 0;
  String currCode = '';
  int type = -1;

  @override
  void initState() {
    super.initState();
    _currController.value = TextEditingValue(text: "00");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 1,
        backgroundColor: Theme.of(context).canvasColor,
        titleSpacing: 0,
        title: Text(
          '数码管',
          style: TextStyle(fontSize: 18),
        ),
      ),
      body: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Expanded(child: ledWidget(), flex: 1),
              Expanded(child: rightWidget(), flex: 1)
            ],
          ),
          SizedBox(height: 10),
          Row(
            children: <Widget>[
              Expanded(
                child: FSuper(
                  height: Comm.screenHeight - 56 - 240 - 20 - 36,
                  width: Comm.screenWidth * 3 / 2 - 20,
                  margin: EdgeInsets.all(10),
                  textAlign: TextAlign.left,
                  padding: EdgeInsets.all(10),
                  backgroundColor: Color(0XFFEEEEEE),
                  corner: Corner.all(6),
                  child1: ListView(
                    controller: _codeController,
                    children: <Widget>[
                      Container(
                        margin: EdgeInsets.all(10),
                        child: Text(currCode),
                      ),
                    ],
                  ),
                  onClick: (){
                    if(currCode.isNotEmpty){
                      Clipboard.setData(ClipboardData(text: currCode));
                      Toast.show("复制成功");
                    }
                  },
                ),
                flex: 3,
              ),
              Expanded(
                child: Container(
                  height: Comm.screenHeight - 56 - 240 - 20 - 36,
                  width: Comm.screenWidth * 2 / 3 - 20,
                  child: Image.asset(
                    jixing == 0 ? Comm.IMG_YING : Comm.IMG_YANG,
                    fit: BoxFit.fill,
                  ),
                ),
                flex: 2,
              ),
            ],
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _currController.dispose();
    _codeController.dispose();
  }

  ///数码管
  Widget ledWidget() {
    return Container(
      width: 168,
      padding: EdgeInsets.all(10),
      alignment: Alignment.center,
      child: Column(
        children: <Widget>[
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              ledItemWidget(20, 80, 'F', 20),
              ledItemWidget(80, 20, 'A', 0),
              ledItemWidget(20, 80, 'B', 20),
            ],
          ),
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              ledItemWidget(20, 80, 'E', 20),
              ledItemWidget(80, 20, 'G', 0),
              ledItemWidget(20, 80, 'C', 20),
            ],
          ),
          Row(
            children: <Widget>[
              Container(width: 20, height: 20),
              ledItemWidget(80, 20, 'D', 0),
              Container(width: 24, height: 20),
              ledItemWidget(22, 22, 'dp', 0),
            ],
          ),
        ],
      ),
    );
  }

  ///数码管断码
  Widget ledItemWidget(
      double width, double height, String num, double offsetTop) {
    Color bgColor, txtColor;
    if (jixing == 0) {
      bgColor =
          mapLeds[num] == 1 ? Theme.of(context).accentColor : Color(0xFFDDDDDD);
      txtColor = mapLeds[num] == 1 ? Colors.white : Colors.grey;
    } else {
      bgColor =
          mapLeds[num] == 0 ? Theme.of(context).accentColor : Color(0xFFDDDDDD);
      txtColor = mapLeds[num] == 0 ? Colors.white : Colors.grey;
    }
    return FSuper(
      width: width,
      height: height,
      backgroundColor: bgColor,
      margin: EdgeInsets.only(top: offsetTop),
      text: num,
      textSize: 12,
      textColor: txtColor,
      textAlignment: Alignment.center,
      corner: Corner.all(width == height ? 20 : 10),
      shadowColor: Color(0xFFDDDDDD),
      shadowBlur: 4,
      onClick: () {
        setState(() {
          mapLeds[num] = mapLeds[num] == 1 ? 0 : 1;
        });
        _currController.value = TextEditingValue(
            text: CommUtil.bin2Hex([
          mapLeds['A'],
          mapLeds['B'],
          mapLeds['C'],
          mapLeds['D'],
          mapLeds['E'],
          mapLeds['F'],
          mapLeds['G'],
          mapLeds['dp'],
        ]));
      },
    );
  }

  ///右侧按钮
  Widget rightWidget() {
    return Container(
      height: 240,
      padding: EdgeInsets.all(10),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          FRadio(
            width: 80,
            height: 30,
            corner: FRadioCorner.all(6),
            border: 1,
            selectedColor: Color(0xFFEEEEEE),
            value: 0,
            child: Text('共阴极'),
            selectedChild: Text(
              '共阴极',
              style: TextStyle(color: Colors.blue),
            ),
            groupValue: jixing,
            onChanged: (value) {
              jixing = value;
              updateUI();
              setState(() {});
            },
          ),
          FRadio(
            width: 80,
            height: 30,
            corner: FRadioCorner.all(6),
            border: 1,
            selectedColor: Color(0xFFEEEEEE),
            value: 1,
            child: Text('共阳极'),
            selectedChild: Text(
              '共阳极',
              style: TextStyle(color: Colors.blue),
            ),
            groupValue: jixing,
            onChanged: (value) {
              jixing = value;
              updateUI();
              setState(() {});
            },
          ),
          FSuper(
            text: '汇编代码',
            width: 100,
            height: 36,
            textAlignment: Alignment.center,
            backgroundColor: Theme.of(context).accentColor,
            corner: Corner.all(6),
            shadowColor: Color(0xFFBBBBBB),
            textColor: Colors.white,
            shadowBlur: 4,
            onClick: () {
              type = 0;
              generCode();
            },
          ),
          FSuper(
            text: 'C语言代码',
            width: 100,
            height: 36,
            textAlignment: Alignment.center,
            backgroundColor: Theme.of(context).accentColor,
            corner: Corner.all(6),
            shadowColor: Color(0xFFBBBBBB),
            textColor: Colors.white,
            shadowBlur: 4,
            onClick: () {
              type = 1;
              generCode();
            },
          ),
          SizedBox(
            width: 100,
            height: 36,
            child: TextField(
              textAlign: TextAlign.center,
              controller: _currController,
              decoration: InputDecoration(
                labelText: "当前值",
                enabled: false,
                hintStyle: TextStyle(fontSize: 12),
                contentPadding: EdgeInsets.all(10),
                border: OutlineInputBorder(
                  borderSide: BorderSide(),
                  borderRadius: BorderRadius.all(Radius.circular(6)),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  void updateUI() {
    mapLeds['A'] = mapLeds['A'] == 1 ? 0 : 1;
    mapLeds['B'] = mapLeds['B'] == 1 ? 0 : 1;
    mapLeds['C'] = mapLeds['C'] == 1 ? 0 : 1;
    mapLeds['D'] = mapLeds['D'] == 1 ? 0 : 1;
    mapLeds['E'] = mapLeds['E'] == 1 ? 0 : 1;
    mapLeds['F'] = mapLeds['F'] == 1 ? 0 : 1;
    mapLeds['G'] = mapLeds['G'] == 1 ? 0 : 1;
    mapLeds['dp'] = mapLeds['dp'] == 1 ? 0 : 1;
    _currController.value = TextEditingValue(
        text: CommUtil.bin2Hex([
      mapLeds['A'],
      mapLeds['B'],
      mapLeds['C'],
      mapLeds['D'],
      mapLeds['E'],
      mapLeds['F'],
      mapLeds['G'],
      mapLeds['dp'],
    ]));
    generCode();
  }

  void generCode() {
    if (type == 0) {
      if (jixing == 0) {
        currCode = "DB  3FH //'0'";
        currCode += "\nDB  06H  //'1'";
        currCode += "\nDB  5BH  //'2'";
        currCode += "\nDB  4FH  //'3'";
        currCode += "\nDB  66H  //'4'";
        currCode += "\nDB  6DH  //'5'";
        currCode += "\nDB  7DH  //'6'";
        currCode += "\nDB  07H  //'7'";
        currCode += "\nDB  7FH  //'8'";
        currCode += "\nDB  6FH  //'9'";
        currCode += "\nDB  77H  //'A'";
        currCode += "\nDB  7CH  //'B'";
        currCode += "\nDB  39H  //'C'";
        currCode += "\nDB  5EH  //'D'";
        currCode += "\nDB  79H  //'E'";
        currCode += "\nDB  71H   //'F'";
      } else {
        currCode = "";
        currCode += "DB  0C0H  //'0'";
        currCode += "\nDB  0F9H  //'1'";
        currCode += "\nDB  0A4H  //'2'";
        currCode += "\nDB  0B0H  //'3'";
        currCode += "\nDB  99H  //'4'";
        currCode += "\nDB  92H  //'5'";
        currCode += "\nDB  82H  //'6'";
        currCode += "\nDB  0F8H  //'7'";
        currCode += "\nDB  80H  //'8'";
        currCode += "\nDB  90H  //'9'";
        currCode += "\nDB  88H  //'A'";
        currCode += "\nDB  83H  //'B'";
        currCode += "\nDB  0C6H  //'C'";
        currCode += "\nDB  0A1H  //'D'";
        currCode += "\nDB  86H  //'E'";
        currCode += "\nDB  8EH  //'F'";
      }
    } else {
      if (jixing == 0) {
        currCode = "unsigned char leddata[]={";
        currCode += "\n0x3F,  //'0'";
        currCode += "\n0x06,  //'1'";
        currCode += "\n0x5B,  //'2'";
        currCode += "\n0x4F,  //'3'";
        currCode += "\n0x66,  //'4'";
        currCode += "\n0x6D,  //'5'";
        currCode += "\n0x7D,  //'6'";
        currCode += "\n0x07,  //'7'";
        currCode += "\n0x7F,  //'8'";
        currCode += "\n0x6F,  //'9'";
        currCode += "\n0x77,  //'A'";
        currCode += "\n0x7C,  //'B'";
        currCode += "\n0x39,  //'C'";
        currCode += "\n0x5E,  //'D'";
        currCode += "\n0x79,  //'E'";
        currCode += "\n0x71  //'F'";
        currCode += "\n};";
      } else {
        currCode = "unsigned char leddata[]={";
        currCode += "\n0xC0,  //'0'";
        currCode += "\n0xF9,  //'1'";
        currCode += "\n0xA4,  //'2'";
        currCode += "\n0xB0,  //'3'";
        currCode += "\n0x99,  //'4'";
        currCode += "\n0x92,  //'5'";
        currCode += "\n0x82,  //'6'";
        currCode += "\n0xF8,  //'7'";
        currCode += "\n0x80,  //'8'";
        currCode += "\n0x90,  //'9'";
        currCode += "\n0x88,  //'A'";
        currCode += "\n0x83,  //'B'";
        currCode += "\n0xC6,  //'C'";
        currCode += "\n0xA1,  //'D'";
        currCode += "\n0x86,  //'E'";
        currCode += "\n0x8E  //'F'";
        currCode += "\n};";
      }
    }
    setState(() {});
  }
}
